package com.scarecrow.blit2d.rasterizer {
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Santiago.J.Elizalde
	 */
	public class AnimationRaster {
		
		private var source:MovieClip;
		private var frames:Array;
		
		private var playing:Boolean;
		private var _looping:Boolean;
		
		//the frame currently being displayed.
		private var _currentFrame:uint;
		//animation frame rate, in frames per second.
		private var _frameRate:uint;
		//length of a single frame, in miliseconds.
		private var _frameLength:uint;
		
		public function AnimationRaster(rasterFrames:Array, frameRate:uint=30) {
			this._frameRate = frameRate;
			this._frameLength = 1000 / frameRate;
			this.frames = rasterFrames;
		}
		
		public function gotoAndPlay(frame:uint):void {
			if (frame >= numFrames) {
				throw new Error("[ERROR] AnimationRaster.gotoAndPlay() :: supplied frame index [" + frame + "] is out of bounds.")
			}
			_currentFrame = frame;
			playing = true;
		}
		public function gotoAndStop(frame:uint):void {
			if (frame >= numFrames) {
				throw new Error("[ERROR] AnimationRaster.gotoAndStop() :: supplied frame index [" + frame + "] is out of bounds.")
			}
			_currentFrame = frame;
			playing = false;
		}
		public function play():void {
			playing = true;
		}
		public function stop():void {
			playing = false;
		}
		public function update(ms:uint = 0):void {
			
			if (!playing) return;
			
			_currentFrame += Math.ceil(ms/(1000/frameRate));
			if (_currentFrame >= numFrames) {
				if (!_looping) {
					_currentFrame = numFrames;
					stop();
				} else {
					_currentFrame = 0;
				}
			}
		}
		private function updateFrame():void {
			//set the current frame
		}
		
		// ----- Getters & setters --------------------------------------
		public function get currentFrameData():void {
			return frames[_currentFrame];
		}
		public function get isPlaying():Boolean {
			return playing;
		}
		public function get numFrames():uint {
			return frames.length;
		}
		public function get currentFrame():uint {
			return _currentFrame;
		}
		public function get looping():Boolean {
			return _looping;
		}
		public function set looping(value:Boolean):void {
			_looping = value;
		}
		public function get frameRate():uint {
			return _frameRate;
		}
	}
}